﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using UCenter.Core.Model;

namespace UCenter.Models
{
    [Table(Name = "dbo.History")]
    public class History : EntityBase
    {
        #region Private Fields

        private int _Id;

        private int? _userId;

        private int _logType;

        private DateTime _logDate;

        private string _message;

        private string _ipAddress;

        private int? _moduleId;

        private EntityRef<Module> _module;

        private EntityRef<User> _user;

        #endregion

        #region Constructor

        public History()
        {
            this._module = default(EntityRef<Module>);
            this._user = default(EntityRef<User>);
        }

        #endregion

        #region Public Properties

        [Column(Name = "ID", Storage = "_Id", DbType = "Int NOT NULL", IsPrimaryKey = true)]
        public int ID
        {
            get
            {
                return this._Id;
            }
            set
            {
                if (this._Id != value)
                {
                    this.SendPropertyChanging("Note");
                    this._Id = value;
                    this.SendPropertyChanged("Note", value);
                }
            }
        }

        [Column(Name = "UserId", Storage = "_userId", DbType = "Int")]
        public int? UserId
        {
            get
            {
                return this._userId;
            }
            set
            {
                if (this._userId != value)
                {
                    if (this._user.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }

                    this.SendPropertyChanging("UserId");
                    this._userId = value;
                    this.SendPropertyChanged("UserId", value);
                }
            }
        }

        [Column(Name = "LogType", Storage = "_logType", DbType = "Int NOT NULL")]
        public int LogType
        {
            get
            {
                return this._logType;
            }
            set
            {
                if (this._logType != value)
                {
                    this.SendPropertyChanging("LogType");
                    this._logType = value;
                    this.SendPropertyChanged("LogType", value);
                }
            }
        }

        [Column(Name = "LogDate", Storage = "_logDate", DbType = "DateTime NOT NULL")]
        public DateTime LogDate
        {
            get
            {
                return this._logDate;
            }
            set
            {
                if (this._logDate != value)
                {
                    this.SendPropertyChanging("LogDate");
                    this._logDate = value;
                    this.SendPropertyChanged("LogDate", value);
                }
            }
        }

        [Column(Name = "Message", Storage = "_message", DbType = "VarChar(4000) NOT NULL", CanBeNull = false)]
        public string Message
        {
            get
            {
                return this._message;
            }
            set
            {
                if (this._message != value)
                {
                    this.SendPropertyChanging("Message");
                    this._message = value;
                    this.SendPropertyChanged("Message", value);
                }
            }
        }

        [Column(Name = "IPAddress", Storage = "_ipAddress", DbType = "VarChar(64)")]
        public string IPAddress
        {
            get
            {
                return this._ipAddress;
            }
            set
            {
                if (this._ipAddress != value)
                {
                    this.SendPropertyChanging("IPAddress");
                    this._ipAddress = value;
                    this.SendPropertyChanged("IPAddress", value);
                }
            }
        }

        [Column(Name = "ModuleId", Storage = "_moduleId", DbType = "Int")]
        public int? ModuleId
        {
            get
            {
                return this._moduleId;
            }
            set
            {
                if (this._moduleId != value)
                {
                    if (this._module.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }

                    this.SendPropertyChanging("ModuleId");
                    this._moduleId = value;
                    this.SendPropertyChanged("ModuleId", value);
                }
            }
        }

        [Association(Name = "Module_History", Storage = "_module", ThisKey = "ModuleId", IsForeignKey = true)]
        public Module Module
        {
            get
            {
                return this._module.Entity;
            }
            set
            {
                Module previousValue = this._module.Entity;
                if (((previousValue != value)
                            || (this._module.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._module.Entity = null;
                        previousValue.Histories.Remove(this);
                    }
                    this._module.Entity = value;
                    if ((value != null))
                    {
                        value.Histories.Add(this);
                        this._moduleId = value.ID;
                    }
                    else
                    {
                        this._moduleId = default(Nullable<int>);
                    }
                }
            }
        }

        [Association(Name = "User_History", Storage = "_user", ThisKey = "UserId", IsForeignKey = true)]
        public User User
        {
            get
            {
                return this._user.Entity;
            }
            set
            {
                User previousValue = this._user.Entity;
                if (((previousValue != value)
                            || (this._user.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._user.Entity = null;
                        previousValue.Histories.Remove(this);
                    }
                    this._user.Entity = value;
                    if ((value != null))
                    {
                        value.Histories.Add(this);
                        this._userId = value.ID;
                    }
                    else
                    {
                        this._userId = default(Nullable<int>);
                    }
                }
            }
        }

        #endregion
    }
}
